import * as fs from 'fs';

const b: Array<{ name: string, fun: string }> = JSON.parse(fs.readFileSync("ydluatype\\war3\\blizzard.json", "utf-8"));
const c: Array<{ name: string, fun: string }> = JSON.parse(fs.readFileSync("ydluatype\\war3\\common.json", "utf-8"));
const d: Array<{ name: string, fun: string }> = JSON.parse(fs.readFileSync("ydluatype\\war3\\DzAPI.json", "utf-8"));
const j: Array<{ name: string, fun: string }> = JSON.parse(fs.readFileSync("ydluatype\\war3\\japi.json", "utf-8"));


function genFun(name: string) {
    fs.readFile(`${name}.txt`, "utf-8", (err, data) => {
        let ft: Array<string> = [];
        let all: Array<string> = []
        let tempResult = `/** @noSelfInFile **/ \n`;
        data.split("\r\n").forEach(line => {
            let lineCon = line.split("=")
            let tempFn = lineCon[0].replace(/"/g, '').trim()
            all.push(tempFn)

            let fnCheck = c.find(e => e.name == tempFn)
                || b.find(e => e.name == tempFn)
                || j.find(e => e.name == tempFn)
                || d.find(e => e.name == tempFn);
            if (fnCheck) {
                // console.log(tempFn, fnCheck)
                // ft.push(`${tempFn}: typeof ${tempFn}`)
                tempResult += fnCheck.fun
            } else {
                tempResult += `declare function ${tempFn}(...option:any[]):any;\n`
                ft.push(tempFn)
            }

        })

        fs.writeFileSync(name + ".d.ts", tempResult);
        // 未找到定义的
        fs.writeFileSync(name + "nofind.json", JSON.stringify(ft, null, " "));
        // 全部
        fs.writeFileSync(name + ".json", JSON.stringify(all, null, " "));
    })
}
console.log()

genFun("./ydluatype/common")
genFun("./ydluatype/japi")